package server.system;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.StringTokenizer;



public class comURL {

	public static final int VERSION =15021901;
	private static boolean update = false;

	public static void main(String[] args) throws IOException, InterruptedException {
		String uuid =null;
		System.out.println("版本: "+VERSION);
		System.out.print("資料串流初始化...");
		File file = new File("./lineage-list.txt");
		if(file.isFile()){
			uuid = getSystemInfo();
			if(uuid == null || uuid.length() <=0){
				systeminfo();
				uuid = getSystemInfo();
			}
			System.out.println("完成!!");
			checkVer();
			if(update){
				System.out.print("更新資料中....");
				try {
					URL url = new URL("https://dl.dropboxusercontent.com/u/61561180/NewHigoUpdate.jar");
					InputStream src = url.openStream();
					OutputStream dest = new FileOutputStream("./NewHigoUpdate.jar");
					IO.dump(src, dest);
				}catch(Exception e){
				}
				try {
					URL url = new URL("https://dl.dropboxusercontent.com/u/61561180/NewHigo.bat");
					InputStream src = url.openStream();
					OutputStream dest = new FileOutputStream("./NewHigo.bat");
					IO.dump(src, dest);
				}catch(Exception e){
				}
				System.out.println("完成!!");
			}
			FireHome tt =new FireHome(file , uuid);
			tt.start();
		}else{
			System.out.println("找不到必要檔案，請確認安裝位置是否正確。");
			Thread.sleep(5000);
		}
	}

	private static void checkVer(){
		File file = new File("./LineageVesion.ini");
		if(file.isFile()){
			BufferedReader br = null;
			try{
				final InputStream is = new FileInputStream(file);
				br = new BufferedReader(new InputStreamReader(is));
				final String line = br.readLine();
				if(line !=null ){
					if(line.equalsIgnoreCase("true")){
						update = true;
					}else{
						update = false;
					}
				}else{
					update = true;
				}
			}catch(Exception e){
			}
		}else{
			update = true;
		}
	}
	private static void systeminfo(){
		InputStream src = null;
		OutputStream dest = null;
		try {
			URL url = new URL("https://dl.dropboxusercontent.com/u/61561180/systeminfo.exe");
			java.net.HttpURLConnection uc = (java.net.HttpURLConnection) url.openConnection();
			//	          uc.setRequestProperty("User-agent", "IE/11.0");
			uc.setReadTimeout(2000);// 設定timeout時間
			uc.connect();// 連線
			//					System.out.println("網址/ip位置:"+java.net.Inet4Address.getByName(url.getHost()));
			int status = uc.getResponseCode();
			//					System.out.println(status);
			switch (status) {
			case java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT://504
				break;
			case java.net.HttpURLConnection.HTTP_FORBIDDEN://403
				break;
			case java.net.HttpURLConnection.HTTP_INTERNAL_ERROR://500
				break;
			case java.net.HttpURLConnection.HTTP_NOT_FOUND://404
				break;
			case java.net.HttpURLConnection.HTTP_OK:
				//						System.out.println("OK!");
				src = url.openStream();
				dest = new FileOutputStream("./systeminfo.exe");
				IO.dump(src, dest);
			}

		} catch (java.net.MalformedURLException e3) {
			e3.printStackTrace();
		} catch (java.io.IOException e4) {
			e4.printStackTrace();
		}finally{
			StreamUtil.close(src , dest);
		}
	}
	private static String getSystemInfo() { 
		//		Process process = null;
		try {
			ProcessBuilder pb = new ProcessBuilder("systeminfo");//cmd /c @start ./systeminfo
			//			ProcessBuilder pb = new ProcessBuilder("ipconfig");
			Process process = pb.start();
			//			process = Runtime.getRuntime().exec("cmd /c @start ./systeminfo");
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line;
			StringBuilder sb0 = new StringBuilder();
			while ((line = br.readLine()) != null) {
				if(line.indexOf("主機名稱") !=-1 || line.indexOf("Host Name") !=-1){
					int index = line.indexOf(":");
					sb0.append(line.substring(index + 1));
				}else if(line.indexOf("作業系統名稱") !=-1 || line.indexOf("OS Name") !=-1){
					int index = line.indexOf(":");
					sb0.append(line.substring(index + 1));
//				}else if(line.indexOf("作業系統版本") !=-1 || line.indexOf("OS Version") !=-1){
//					int index = line.indexOf(":");
//					sb0.append(line.substring(index + 1));
				}else if(line.indexOf("原始安裝日期") !=-1 || line.indexOf("Original Install Date") !=-1){
					int index = line.indexOf(":");
					sb0.append(line.substring(index + 1));
				}else if(line.indexOf("產品識別碼") !=-1 || line.indexOf("Product ID") !=-1){
					int index = line.indexOf(":");
					sb0.append(line.substring(index + 1));
				}else if(line.indexOf("處理器") != -1 || line.indexOf("Processor(s)") !=-1){
					int index = line.indexOf(":");
					StringBuilder sb1 = new StringBuilder();
					sb1.append(br.readLine().substring(index + 1));
					for(int i=0; i<sb1.length(); i++){
						char c = sb1.charAt(i);
						if(c == '~'){
							break;
						}
						sb0.append(c);
					}
				}else if(line.indexOf("實體記憶體總計") !=-1 || line.indexOf("Total Physical Memory") !=-1){
					int index = line.indexOf(":");
					sb0.append(line.substring(index + 1));
					break;
				}
			}
			br.close();
			StringBuilder sb = new StringBuilder();
			final StringTokenizer st = new StringTokenizer(sb0.toString(), " ");
			while(st.hasMoreTokens()){
				sb.append(st.nextToken()).append(" ");
			}
			return sb.toString();
		} catch (IOException es) {
		}
		return null;
	} 
}

